package com.iris.live.services.services.impl;

import com.iris.live.services.common.reconsitution.SendMsgRoleUtil;
import com.iris.live.services.data.repositories.ContractRepository;
import com.iris.live.services.interceptors.SessionInterceptor;
import com.iris.live.services.services.ContractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ContractServiceImpl implements ContractService {

    private Logger logger = LoggerFactory.getLogger(SendMsgRoleUtil.class);

    @Autowired
    private ContractRepository contractRepository;

    @Override
    public List getContractInfo(String proId) {
        return contractRepository.getContractInfo(proId);
    }

    @Override
    public List getCarOrderContract(String proId, String permit) {
        return contractRepository.getCarOrderContract(proId, permit);
    }

    public List getKobeJamesOne(String proId, String permit) {
        return contractRepository.getKobeJamesOne(proId, permit);
    }


    public List getKobeJamesTwo(String proId, String permit) {
        return contractRepository.getKobeJamesTwo(proId, permit);
    }

    public List getKobeJamesThree(String proId, String permit) {
        return contractRepository.getKobeJamesThree(proId, permit);
    }

    @Override
    public List getCarSaleContract(String proId, String permit) {
        return contractRepository.getCarSaleContract(proId, permit);
    }

    @Override
    public List getCarSaleAttachment(String proId, HttpServletRequest request) {
        String permit = SessionInterceptor.getLicense(request);
        List list = contractRepository.getCarSaleAttachment(proId, permit);
        List list2 = new ArrayList();
        Map map2 = new LinkedHashMap();
        String strJingPing = "";
        Map m = new HashMap();
        Map m2 = new HashMap();
        List l = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            if (!"".equals(map.get("厂家贴息")) && map.get("厂家贴息") != null) {
                BigDecimal d1 = BigDecimal.valueOf(Double.valueOf(map.get("厂家贴息").toString()));
                if (d1.compareTo(BigDecimal.ZERO) == 1) {//有效贴息
                    map2.put("厂家贴息", "贴息 " + map.get("厂家贴息") + "元");
                } else {
                    logger.warn("无效的厂家贴息");
                }
            }

            if ("保险".equals(map.get("项目")) && "厂家".equals(map.get("随车类型"))) {
                map2.put("厂家保险", map.get("内容"));
            }

            if("厂家".equals(map.get("随车类型"))) {
                if ("SSSC".equals(map.get("项目")) || "旅游礼包".equals(map.get("项目")) || "SD卡".equals(map.get("项目")) || "加油卡".equals(map.get("项目")) || "实物礼品".equals(map.get("项目")) || "服务礼券".equals(map.get("项目"))) {
                    map2.put(map.get("项目"), map.get("内容"));
                }
            }

            //获得内容列最后一个字符
            String lastStr=null;
            if(!"".equals(map.get("内容")) && map.get("内容") != null){
                lastStr = map.get("内容").toString().substring(map.get("内容").toString().length() - 1, map.get("内容").toString().length());
            }

            if ("经销商".equals(map.get("随车类型")) && "销售部".equals(map.get("随车部门"))) {
                if ("保险".equals(map.get("项目"))) {
                    //String lastStr=map.get("内容").toString().substring(map.get("内容").toString().length()-1,map.get("内容").toString().length());
                    map2.put("经销商保险", "保险 " + ("元".equals(lastStr) ? map.get("内容") : map.get("内容") + "元"));
                }
                if ("贴息".equals(map.get("项目"))) {
                    map2.put("经销商贴息", "贴息 " + ("元".equals(lastStr) ? map.get("内容") : map.get("内容") + "元"));
                }
                if ("A保".equals(map.get("项目"))) {
                    map2.put("A保", "A保 " + ("次".equals(lastStr) ? ("0.00".equals(map.get("内容")) ? 0 : map.get("内容")) : ("0.00".equals(map.get("内容")) ? 0 : map.get("内容")) + "次"));
                }
                if ("B保".equals(map.get("项目"))) {
                    map2.put("B保", "B保 " + ("次".equals(lastStr) ? ("0.00".equals(map.get("内容")) ? 0 : map.get("内容")) : ("0.00".equals(map.get("内容")) ? 0 : map.get("内容")) + "次"));
                }
                if ("尚银卡".equals(map.get("项目"))) {
                    map2.put("尚银卡", "尚银卡 " + map.get("内容"));
                }
                if ("逸蓝卡".equals(map.get("项目"))) {
                    map2.put("逸蓝卡", "逸蓝卡 " + map.get("内容"));
                }
                if ("加油卡".equals(map.get("项目"))) {
                    map2.put("经销商加油卡", "加油卡 价值" + ("元".equals(lastStr) ? map.get("内容") : map.get("内容") + "元"));
                }
                if ("购物卡".equals(map.get("项目"))) {
                    map2.put("购物卡", "购物卡 价值" + ("元".equals(lastStr) ? map.get("内容") : map.get("内容") + "元"));
                }
            }

            if (!"".equals(map.get("售价为0的精品")) && map.get("售价为0的精品") != null) {
                int num1 = Integer.parseInt(map.get("售价为0的精品数量").toString());
                BigDecimal num2 = BigDecimal.valueOf(Double.valueOf(map.get("售价为0的精品价值").toString()));
                strJingPing = map.get("售价为0的精品") + "*" + map.get("售价为0的精品数量") + " 价值" + num2.multiply(new BigDecimal(num1)) + "元 ,,";
                m.put(map.get("售价为0的精品"), strJingPing);
                m2.put(m.size(), map.get("售价为0的精品"));
            }

            if (i == list.size() - 1) {
                if (!"".equals(map.get("售价为0的五星延保")) && map.get("售价为0的五星延保") != null) {
                    map2.put("售价为0的五星延保", "五星延保 " + ("元".equals(lastStr) ? map.get("售价为0的五星延保") : map.get("售价为0的五星延保") + "元"));
                }

                //合同编号按照订单编号的规则生成
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                if (!"".equals(map.get("订单号")) && map.get("订单号") != null) {
                    map2.put("合同号", map.get("订单号"));
                } else if (!"".equals(map.get("合同签约日期")) && map.get("合同签约日期") != null) {
                    map2.put("合同号", permit + sdf.format(map.get("合同签约日期")) + proId);
                } else {
                    map2.put("合同号", "");
                    logger.warn("合同号为空");
                }

                if (!"".equals(map.get("经销商全称")) && map.get("经销商全称") != null) {
                    map2.put("卖家", map.get("经销商全称"));
                } else {
                    map2.put("卖家", "");
                    logger.warn("卖家为空");
                }


                if (!"".equals(map.get("开票客户姓名")) && map.get("开票客户姓名") != null) {
                    map2.put("买家", map.get("开票客户姓名"));
                } else if (!"".equals(map.get("订单客户姓名")) && map.get("订单客户姓名") != null) {
                    map2.put("买家", map.get("订单客户姓名"));
                } else if (!"".equals(map.get("顾客姓名")) && map.get("顾客姓名") != null) {
                    map2.put("买家", map.get("顾客姓名"));
                } else {
                    map2.put("买家", "");
                    logger.warn("买家为空");
                }

                if (!"".equals(map.get("合同签约日期")) && map.get("合同签约日期") != null) {
                    map2.put("日期", map.get("合同签约日期"));
                } else {
                    map2.put("日期", "");
                    logger.warn("合同签约日期为空");
                }
            }

        }

        strJingPing = "";
        for (int i = 0; i < m.size(); i++) {
            strJingPing += m.get(m2.get(i + 1));
        }

        if (strJingPing.length() != 0) {
            strJingPing = strJingPing.substring(0, strJingPing.length() - 2);
            map2.put("售价为0的精品", strJingPing);
        }
        list2.add(map2);
        return list2;
    }

    @Override
    public List getInstallationContract(String proId, String permit) {
        return contractRepository.getInstallationContract(proId, permit);
    }

    @Override
    public List getCarSaleAttachmentWithLoss(String proId) {
        return contractRepository.getCarSaleAttachmentWithLoss(proId);
    }

    @Override
    public List getPickCarWarrant(String proId) {
        return contractRepository.getPickCarWarrant(proId);
    }

    @Override
    public List getProxyAgreement(String proId, String permit) {
        return contractRepository.getProxyAgreement(proId, permit);
    }

    @Override
    public List getInvoiceAdvice(String proId, String permit) {
        return contractRepository.getInvoiceAdvice(proId, permit);
    }

    @Override
    public List getPayInsuranceProxy(String proId, String permit) {
        return contractRepository.getPayInsuranceProxy(proId, permit);
    }

    @Override
    public List getSecurityAgreement(String proId, String permit) {
        return contractRepository.getSecurityAgreement(proId, permit);
    }

}
